package me.shurufa.holder;

import android.content.Context;
import android.content.Intent;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import me.shurufa.R;
import me.shurufa.event.EventObject;
import me.shurufa.model.Digest;
import me.shurufa.model.DigestResponse;
import me.shurufa.net.NetworkConstants;
import me.shurufa.net.RequestServerTask;
import me.shurufa.utils.Constants;
import me.shurufa.utils.Global;
import me.shurufa.utils.HttpUtil;
import me.shurufa.utils.Utils;

/**
 * My BookInfo中 相对于我的书摘 支持修改状态操作
 *
 * @FileName: me.shurufa.holder.HorizationMyMarkViewHolder.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-02-05 17:21
 */
public class HorizationMyMarkViewHolder extends BaseViewHolder{

    private Context mContext;
    private Digest mDigest;

    @Bind(R.id.mark_state)
    TextView markState;
    @Bind(R.id.mark_digest)
    TextView markDigest;
    @Bind(R.id.mark_from)
    TextView markFrom;
    @Bind(R.id.mark_note)
    TextView markNote;
    @Bind(R.id.btn_write_note)
    Button btnWriteNote;

    //mark from
    String date;
    String pageNumberFrom;
    String partName;

    public HorizationMyMarkViewHolder(View itemView, Object parent) {
        super(itemView, parent);
        mContext = itemView.getContext();
        ButterKnife.bind(this, itemView);
        btnWriteNote.setOnClickListener(this);
        markState.setOnClickListener(this);
    }

    public void bind(Digest digest){
        if (digest==null)
            return;
        mDigest = digest;
        if (!TextUtils.isEmpty(digest.content))
            markDigest.setText(digest.content);
        else
            markDigest.setText("");
        if (!TextUtils.isEmpty(digest.note.trim())) {
            btnWriteNote.setVisibility(View.GONE);
            markNote.setVisibility(View.VISIBLE);
            markNote.setText(digest.note);
        }else {
            btnWriteNote.setVisibility(View.VISIBLE);
            markNote.setVisibility(View.GONE);
        }
        if (digest.publish_state.equals("publish")){
            markState.setText(mContext.getString(R.string.has_published));
        }else if (digest.publish_state.equals("save"))
            markState.setText(mContext.getString(R.string.not_publish));
        else
            markState.setVisibility(View.GONE);
        setMarkFrom(digest);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        //TODO 需要增加点击事件
        switch (v.getId()){
            case R.id.mark_state:
                new RequestServerTask<DigestResponse>(DigestResponse.class, mContext, mContext.getString(R.string.commit_pls_wait)) {

                    @Override
                    protected String requestServer() {
                        Map<String, Object> params = new HashMap<>();
                        params.put("token", Global.currentUser.token);
                        params.put("excerptId",mDigest.id);
                        params.put("content",mDigest.content);
                        if (!TextUtils.isEmpty(mDigest.note)) {
                            params.put("note", mDigest.note);
                        }else params.put("note","");
                        params.put("publishState",mDigest.publish_state.equals("publish")?"save":"publish");
                        return HttpUtil.post(NetworkConstants.MODIFY_DIGEST, params);
                    }

                    @Override
                    protected void onSuccess(DigestResponse result) {
                        if (result.success) {
                            EventObject object = new EventObject();
                            object.setEventAction(Constants.ACTION_MODIFY_MY_DIGEST);
                            object.setDigest(result.data);
                            EventBus.getDefault().post(object);
                        }
                    }
                }.start();
                break;
            case R.id.btn_write_note:
                EventObject object = new EventObject();
                object.setEventAction(Constants.ACTION_WRITE_NOTE);
                object.setDigest(mDigest);
                EventBus.getDefault().post(object);
                break;
        }
    }

    private void setMarkFrom(Digest digest){


        if (!TextUtils.isEmpty(digest.created_at))
            date = Utils.tranformDate(digest.created_at)+" ";
        else  date="";

        if (digest.menu_info!=null)
            partName = "\n"+String.format(mContext.getString(R.string.name_of_part),digest.menu_info.title);
        else partName = "\n"+mContext.getString(R.string.name_of_part);


        if (digest.page_num!=0)
            pageNumberFrom = String.format(mContext.getString(R.string.page_of_book),String.valueOf(digest.page_num));
        else pageNumberFrom = "";

        SpannableString span = new SpannableString(partName);
        span.setSpan(new RelativeSizeSpan(0.7f), 0, span.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

        markFrom.setText(TextUtils.concat(date,pageNumberFrom,span));

    }
}
